<?php
// +----------------------------------------------------------------------
// | API路由 - admin
// +----------------------------------------------------------------------
use think\facade\Route;

use app\middleware\{
    // 路由访问认证中间件
    RouteAccessCheck,
    // 路由日志记录中间件
    RouteLogRecord,
    // 路由TOKEN认证中间件
    RouteTokenAuthorize,
    // 路由TOKEN解析中间件
    RouteTokenParse
};

Route::group('api/admin', function () {

    // 用户登录
    Route::post('access/login', 'access/login')->append([
        'record' => [
            'type' => 'login',
            'summary' => '用户登录系统'
        ]
    ]);

    // 刷新Token
    Route::post('access/refresh-token', 'access/refreshToken');

    // 已登录用户路由
    Route::group(function () {

        // 获取用户信息
        Route::get('access/info', 'access/getUserInfo');

        // 修改用户信息
        Route::put('access/info', 'access/setUserInfo')->append([
            'record' => [
                'type' => 'user-info',
                'summary' => '修改用户信息'
            ]
        ]);

        // 修改用户密码
        Route::put('access/password', 'access/setUserPassword')->append([
            'record' => [
                'type' => 'user-info',
                'summary' => '修改用户密码'
            ]
        ]);

        // 获取用户路由
        Route::get('access/routes', 'access/getAsyncRoutes');

        // 获取用户系统消息
        Route::get('access/messages', 'access/queryUserMsg');

        // 获取用户日志分页列表
        Route::post('access/logs/page', 'access/getUserLogsPageList');

        // 权限中间件
        Route::group(function () {

            // 系统消息（用户）
            Route::group('messages', function () {

                // 获取分页列表
                Route::post('page', 'messages/getPageList');

                // 标记已读状态
                Route::put(':id/read', 'messages/setRead');

                // 全部已读
                Route::put('read', 'messages/setReadAll');

                // 删除信息
                Route::delete(':id', 'messages/delete');

                // 批量删除
                Route::delete('', 'messages/batchDelete');
            });

            // 表格模板
            Route::group('template/table', function () {

                // 获取完整分类
                Route::get('catalogs', 'template.table/getCatalogs');

                // 获取分页列表
                Route::post('page', 'template.table/getPageList');

                // 创建信息
                Route::post('', 'template.table/create')->append([
                    'record' => [
                        'type' => 'template-table',
                        'summary' => '创建表格模板'
                    ]
                ]);

                // 获取信息
                Route::get(':id', 'template.table/read');

                // 更新信息
                Route::put(':id', 'template.table/update')->append([
                    'record' => [
                        'type' => 'template-table',
                        'summary' => '修改表格模板'
                    ]
                ]);

                // 切换状态
                Route::put(':id/status', 'template.table/setStatus')->append([
                    'record' => [
                        'type' => 'template-table',
                        'summary' => '修改表格模板'
                    ]
                ]);

                // 删除信息
                Route::delete(':id', 'template.table/delete')->append([
                    'record' => [
                        'type' => 'template-table',
                        'summary' => '删除表格模板'
                    ]
                ]);

                // 批量删除
                Route::delete('', 'template.table/batchDelete')->append([
                    'record' => [
                        'type' => 'template-table',
                        'summary' => '删除表格模板'
                    ]
                ]);
            });

            // 系统管理
            Route::group('system', function () {
                // 用户管理
                Route::group('users', function () {

                    // 获取可选角色
                    Route::get('roles', 'system.users/getRolesList');

                    // 获取可选部门
                    Route::get('depts', 'system.users/getDeptsList');

                    // 获取分页列表
                    Route::post('page', 'system.users/getPageList');

                    // 创建信息
                    Route::post('', 'system.users/create')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '创建系统用户'
                        ]
                    ]);

                    // 获取信息
                    Route::get(':id', 'system.users/read');

                    // 更新信息
                    Route::put(':id', 'system.users/update')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '更新系统用户'
                        ]
                    ]);

                    // 更新密码
                    Route::put(':id/password', 'system.users/setPassword')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '更新系统用户'
                        ]
                    ]);

                    // 更新角色
                    Route::put(':id/roles', 'system.users/setRoles')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '更新系统用户'
                        ]
                    ]);

                    // 更新状态
                    Route::put(':id/status', 'system.users/setStatus')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '更新系统用户'
                        ]
                    ]);

                    // 删除信息
                    Route::delete(':id', 'system.users/delete')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '删除系统用户'
                        ]
                    ]);

                    // 批量删除
                    Route::delete('', 'system.users/batchDelete')->append([
                        'record' => [
                            'type' => 'system-users',
                            'summary' => '删除系统用户'
                        ]
                    ]);
                });

                // 角色管理
                Route::group('roles', function () {

                    // 获取可选菜单
                    Route::get('menus', 'system.roles/getMenusList');

                    // 获取分页列表
                    Route::post('page', 'system.roles/getPageList');

                    // 创建信息
                    Route::post('', 'system.roles/create')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '创建系统角色'
                        ]
                    ]);

                    // 获取信息
                    Route::get(':id', 'system.roles/read');

                    // 更新信息
                    Route::put(':id', 'system.roles/update')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '更新系统角色'
                        ]
                    ]);

                    // 更新菜单
                    Route::put(':id/permissions', 'system.roles/setPermissions')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '更新系统角色'
                        ]
                    ]);

                    // 更新状态
                    Route::put(':id/status', 'system.roles/setStatus')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '更新系统角色'
                        ]
                    ]);

                    // 删除信息
                    Route::delete(':id', 'system.roles/delete')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '删除系统角色'
                        ]
                    ]);

                    // 批量删除
                    Route::delete('', 'system.roles/batchDelete')->append([
                        'record' => [
                            'type' => 'system-roles',
                            'summary' => '删除系统角色'
                        ]
                    ]);
                });

                // 菜单管理
                Route::group('menus', function () {

                    // 获取完整列表
                    Route::get('', 'system.menus/getList');

                    // 创建信息
                    Route::post('', 'system.menus/create')->append([
                        'record' => [
                            'type' => 'system-menus',
                            'summary' => '创建系统菜单'
                        ]
                    ]);

                    // 获取信息
                    Route::get(':id', 'system.menus/read');

                    // 更新信息
                    Route::put(':id', 'system.menus/update')->append([
                        'record' => [
                            'type' => 'system-menus',
                            'summary' => '更新系统菜单'
                        ]
                    ]);

                    // 更新状态
                    Route::put(':id/status', 'system.menus/setStatus')->append([
                        'record' => [
                            'type' => 'system-menus',
                            'summary' => '更新系统菜单'
                        ]
                    ]);

                    // 删除信息
                    Route::delete(':id', 'system.menus/delete')->append([
                        'record' => [
                            'type' => 'system-menus',
                            'summary' => '删除系统菜单'
                        ]
                    ]);

                    // 批量删除
                    Route::delete('', 'system.menus/batchDelete')->append([
                        'record' => [
                            'type' => 'system-menus',
                            'summary' => '删除系统菜单'
                        ]
                    ]);
                });

                // 部门管理
                Route::group('depts', function () {

                    // 获取完整列表
                    Route::get('', 'system.depts/getList');

                    // 创建信息
                    Route::post('', 'system.depts/create')->append([
                        'record' => [
                            'type' => 'system-depts',
                            'summary' => '创建系统部门'
                        ]
                    ]);

                    // 获取信息
                    Route::get(':id', 'system.depts/read');

                    // 更新信息
                    Route::put(':id', 'system.depts/update')->append([
                        'record' => [
                            'type' => 'system-depts',
                            'summary' => '更新系统部门'
                        ]
                    ]);

                    // 更新状态
                    Route::put(':id/status', 'system.depts/setStatus')->append([
                        'record' => [
                            'type' => 'system-depts',
                            'summary' => '更新系统部门'
                        ]
                    ]);

                    // 删除信息
                    Route::delete(':id', 'system.depts/delete')->append([
                        'record' => [
                            'type' => 'system-depts',
                            'summary' => '删除系统部门'
                        ]
                    ]);

                    // 批量删除
                    Route::delete('', 'system.depts/batchDelete')->append([
                        'record' => [
                            'type' => 'system-depts',
                            'summary' => '删除系统部门'
                        ]
                    ]);
                });
            });
        })->middleware(RouteAccessCheck::class);
    })->middleware(RouteTokenAuthorize::class);
})->middleware([RouteTokenParse::class, RouteLogRecord::class])->prefix('admin.')->pattern([
    // 路由参数统一验证
    'name' => '.*',
    'id' => '\d+',
    'ids' => '[0-9,()]+',
])->append([
    // 请求类型
    'type' => 'admin',
    // 标记为API请求
    'api-request' => true
]);
